
package com.bear.cmcc.NetTestHTTP.service;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.PowerLock;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

/**
 * 这个主要用来下载全国数据排名数据，和福建小区排名数据。
 * 
 * @author suchangjun
 */
public class NetSpeedInfoService extends Service {

    private PowerLock powerLock;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
        try {
            powerLock = new PowerLock(this, "NetSpeedInfoService");
            startDownFile(this);
        } catch (Exception e) {
            if (e != null) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 启动一个下载文件进程。
     * 
     * @param context
     */
    private void startDownFile(Context context) {
        if (powerLock != null) {
            powerLock.acquireWakeLock();
        }
        MyCommonUtil.showMessage(NetSpeedInfoService.this, R.string.start_load_data);
        Thread thread = new Thread() {

            @Override
            public void run() {
                boolean success = false;
                boolean successFj = false;
                try {                    
                    success = NetSpeedInfoServiceUtil
                            .beginDownCountryNetWorkFile(NetSpeedInfoService.this);
                    successFj = NetSpeedInfoServiceUtil
                            .beginDownFjNetWorkFile(NetSpeedInfoService.this);
                } catch (Exception e) {
                    if (e != null) {
                        e.printStackTrace();
                    }
                } finally {
                    sendFileDownBroad(success, successFj);
                    if (powerLock != null) {
                        powerLock.releaseWakeLock();
                    }
                    stopSelf();// 数据下载完成，把自己这个服务停止。
                }

            }
        };
        thread.start();

    }

    /**
     * 文件下载完成发送一个广播通知activity.
     * 
     * @param AllFileSuccess 全国排名数据下次成功与否。
     * @param fjFileSuccess 福建小区排名下载成功与否。
     */
    private void sendFileDownBroad(boolean AllFileSuccess, boolean fjFileSuccess) {
        try {
            Intent intent = new Intent(MyCommonConstant.PrivateActionName.NET_WORK_TEST_DATA_DOWN);
            intent.putExtra(MyCommonConstant.PrivateActionName.ALL_DATA_SUCCESS, AllFileSuccess);
            intent.putExtra(MyCommonConstant.PrivateActionName.FJ_DATA_SUCCESS, fjFileSuccess);

            sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void onDestroy() {
    	Log.i("NetSpeedInfoService", "NetSpeedInfoService=>ondestroy");
        try {
            if (powerLock != null) {
                powerLock.releaseWakeLock();
            }
            powerLock = null;
        } catch (Exception e) {
            if (e != null) {
                e.printStackTrace();
            }
        }
        super.onDestroy();
    }

}
